package com.ctg.sysmgr.portal.gen;

import java.io.File;
import java.io.IOException;
import java.sql.SQLException;
import java.util.Collection;
import java.util.List;
import java.util.Map;

import org.apache.commons.io.FileUtils;

import com.ctg.sysmgr.portal.gen.utils.FreemarkerUtils;
import com.github.abel533.database.Dialect;
import com.github.abel533.database.IntrospectedColumn;
import com.github.abel533.database.IntrospectedTable;
import com.github.abel533.database.SimpleDataSource;
import com.github.abel533.utils.DBMetadataUtils;
import com.google.common.collect.Maps;

/**
 * Hello world!
 *
 */
public class App {
	private final static String OUT_PUT_FILE = "output";

	public static void main(String[] args) throws IOException {
		SimpleDataSource dataSource = new SimpleDataSource(Dialect.MYSQL,
				"jdbc:mysql://localhost:3306/test", "root", "");
		DBMetadataUtils dbMetadataUtils = null;
		try {
			dbMetadataUtils = new DBMetadataUtils(dataSource);

			List<IntrospectedTable> list = dbMetadataUtils
					.introspectTables(dbMetadataUtils.getDefaultConfig());

//			for (IntrospectedTable table : list) {
//				System.out.println(table.getName() + ":");
//				for (IntrospectedColumn column : table.getAllColumns()) {
//					System.out.println(column.getName()
//							+ " - "
//							+ column.getJdbcTypeName()
//							+ " - "
//							+ column.getJavaProperty()
//							+ " - "
//							+ column.getJavaProperty()
//							+ " - "
//							+ column.getFullyQualifiedJavaType()
//									.getFullyQualifiedName() + " - "
//							+ column.getRemarks());
//				}
//			}
			Map<String, Object> map = Maps.newHashMap();
			map.put("tableList", list);
			
			File path = new File("src/main/resources/conf/templates");
			Collection<File> listFiles = FileUtils.listFiles(path, new String[]{"tpl"}, false);
			for (File file : listFiles) {
				String tplName = file.getName();
				String javaName = tplName.replace(".tpl", ".js");
				File file1 = new File(OUT_PUT_FILE + File.separator + javaName);// 设定为当前文件夹
				FileUtils.write(file1, "");
				FreemarkerUtils.generateToFile(path.getAbsolutePath(), tplName,
						file1.getAbsolutePath(), map, null);
			}
			
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
}
